/**
 * @Description: test
 * @Author: Hu Outstanding
 * @Date: 2022/1/10 23:55
 */
package com.aaa.three.service.impl;

import com.aaa.three.mapper.AdminMapper;
import com.aaa.three.mapper.AdminPermisssionMapper;
import com.aaa.three.mapper.LoginMapper;
import com.aaa.three.pojo.domain.AdminInfo;
import com.aaa.three.pojo.domain.AdminLogin;
import com.aaa.three.pojo.domain.AdminPermission;
import com.aaa.three.pojo.vo.AdminVO;
import com.aaa.three.result.AjaxResult;
import com.aaa.three.service.AdminService;
import com.aaa.three.sysconst.LoginConst;
import com.aaa.three.util.PermissionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.security.Permission;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private HttpSession session;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminPermisssionMapper adminPermisssionMapper;

    @Override
    public AjaxResult login(String username, String password) {
        AdminLogin login = loginMapper.isLogin(username,password);
        if (login == null){
            return AjaxResult.fail(LoginConst.EMP_STATE_ZHANGHAOCUOWU);
        }
        if (login.getAstate().equals(LoginConst.EMP_STATE_DONGJIE)){
            return AjaxResult.fail(LoginConst.EMP_STATE_ZHANGHAODONGJIE);
        }
        if (login.getAstate().equals(LoginConst.EMP_STATE_LIZHI)){
            return AjaxResult.fail(LoginConst.EMP_STATE_YUANGONGLIZHI);
        }
        Integer aid = login.getAid();
        AdminInfo adminInfo = adminMapper.getAdminByID(aid);
        if (adminInfo.getDept().getState().equals(LoginConst.EMP_STATE_UN_USE)){
            return AjaxResult.fail(LoginConst.EMP_STATE_BUMENWEIQIYONG);
        }
        if (adminInfo.getOffice().getState().equals(LoginConst.EMP_STATE_UN_USE)){
            return AjaxResult.fail(LoginConst.EMP_STATE_KESHIWEIQIYONG);
        }
        session.setAttribute(LoginConst.EMP_IN_SESSION,adminInfo);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getInfo() {
        AdminInfo admin = (AdminInfo) session.getAttribute(LoginConst.EMP_IN_SESSION);
        HashMap<Object, Object> data = new HashMap<>();
        data.put("currentAdmin",admin);
        if (admin.getRole().getState() == 1){
            if (admin.getRole().getId() == 1){
                List<AdminPermission> superAdminPermission = adminPermisssionMapper.getSuperAdminPermission();
                data.put("currentPermission",superAdminPermission);
            }else {
                List<Map<Object, Object>> normalAdminPermission = adminPermisssionMapper.getNormalAdminPermission(admin.getRole().getId());
                List<Map<Object, Object>> haha = PermissionUtil.haha(normalAdminPermission);
                data.put("currentPermission",haha);
            }
        }
        return AjaxResult.success(data);
    }

    @Override
    public AjaxResult checkPass(AdminLogin adminLogin) {
        if (adminLogin.getApassword().equals(adminLogin.getAgainPassword())){
            int i = adminMapper.checkPass(adminLogin);
            if (i == 1){
                return AjaxResult.success();
            }
            return AjaxResult.fail("请确认原始密码是否输入正确");
        }else {
            return AjaxResult.fail("请却确认两次输入密码是否一致");
        }

    }

    @Override
    public AjaxResult updatAdminInfo(AdminVO adminVO) {
        int i = adminMapper.updatAdminInfo(adminVO);
        if (i == 1){
            return AjaxResult.success();
        }
        return AjaxResult.fail("信息修改失败");
    }

    @Override
    public AjaxResult updateHeadImg(AdminVO adminVO) {
        AdminInfo admin = (AdminInfo) session.getAttribute(LoginConst.EMP_IN_SESSION);
        int id = admin.getId();
        String headImg = adminVO.getAphoto();

        int i = adminMapper.updateHeadImg(headImg,id);
        if (i > 0){
            return AjaxResult.success();
        }
        return AjaxResult.fail("头像上传失败");


    }
}
